package table

import (
	. "go_tempate/SQL/gormDB"
	"gorm.io/gorm"
)

// 登录信息表
// ——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
type LoginInfo struct {
	gorm.Model
	Account  string `grom:"type:varchar(20);not null;unique;comment:'登录账号'"`
	Password string `grom:"type:tinytext;not null;comment:'登录密码'"`
	State    int    `gorm:"type:int;default:0;not null;comment:'账号状态 0 未登录 1 登陆  2 冻结'"`
}

// 查询全部登录信息
// ----------------------------------------------------------------------------------------------------------
func (l *LoginInfo) AllLoginInfo() ([]LoginInfo, error) {
	var loginInfos = make([]LoginInfo, 0)
	err := Gorm.Find(&loginInfos).Error
	return loginInfos, err
}

// 按照id查询查询单个用户信息
// ----------------------------------------------------------------------------------------------------------
func (l *LoginInfo) OnceLoginInfoInId(id uint) error {
	return Gorm.Where("id = ?", id).First(l).Error
}

// 按照登陆账户查询查询单个用户信息
// ----------------------------------------------------------------------------------------------------------
func (l *LoginInfo) OnceLoginInfoInAccount(account string) error {
	return Gorm.Where("account = ?", account).First(l).Error
}

// 修改用户登陆密码与登陆状态
// ----------------------------------------------------------------------------------------------------------
func (l *LoginInfo) SetLoginInfoPassword(password string) error {
	return Gorm.Model(&LoginInfo{}).Where("id = ?", l.ID).Update("password", password).Update("state", 0).Error
}

// 修改查询用户的登陆状态
// ----------------------------------------------------------------------------------------------------------
func (l *LoginInfo) SetLoginInfoState(state int) error {
	return Gorm.Model(&LoginInfo{}).Where("id = ?", l.ID).Update("state", state).Error
}
